<?php
class ComentariosController extends AppController {

	var $name = 'Comentarios';
	var $helpers = array('Html', 'Form', 'Javascript','Flash');



	function add() {
		if (!empty($this->data)) {
			$this->Comentario->create();
			if ($this->Comentario->save($this->data)) {
				$this->Session->setFlash(__('Se guardo el comentario', true));
				$this->redirect(array('controller'=>'noticias','action'=>'view',$this->data['Comentario']['noticia_id']));
			} else {
				$this->Session->setFlash(__('The Comentario could not be saved. Please, try again.', true));
			}
		}
		$noticias = $this->Comentario->Noticia->find('list');
		$this->set(compact('noticias'));
	}


	function admin_index() {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout = "admin";
		$this->Comentario->recursive = 0;
		$this->set('comentarios', $this->paginate());
	}

	function admin_view($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout = "admin";
		if (!$id) {
			$this->Session->setFlash(__('Invalid Comentario.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('comentario', $this->Comentario->read(null, $id));
	}

	function admin_add() {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout = "admin";
		if (!empty($this->data)) {
			$this->Comentario->create();
			if ($this->Comentario->save($this->data)) {
				$this->Session->setFlash(__('The Comentario has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Comentario could not be saved. Please, try again.', true));
			}
		}
		$noticias = $this->Comentario->Noticia->find('list');
		$this->set(compact('noticias'));
	}

	function admin_edit($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
	$this->layout = "admin";
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Comentario', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Comentario->save($this->data)) {
				$this->Session->setFlash(__('The Comentario has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Comentario could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Comentario->read(null, $id);
		}
		$noticias = $this->Comentario->Noticia->find('list');
		$this->set(compact('noticias'));
	}

	function admin_delete($id = null) {
	if(isset($this->params['admin']) && $this->params['admin'])
	{
			// check that a user is logged in
			    $this->check_user();
	}
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Comentario', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Comentario->del($id)) {
			$this->Session->setFlash(__('Comentario deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
